{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.7320508075688772"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def squareRoot(n):\n",
    "    root = n/2\n",
    "    for i in range(20):\n",
    "        root = 0.5 * (root+n/root)\n",
    "    \n",
    "    return root\n",
    "\n",
    "squareRoot(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "121645100408832000\n"
     ]
    }
   ],
   "source": [
    "import functools\n",
    "res = functools.reduce(lambda x,y: x*y, range(1,20) )\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Queue(object):\n",
    "    def __init__(self):\n",
    "        self.items=[]\n",
    "    def isEmpty(self):\n",
    "        return True if len(self.items)==0 else False\n",
    "    def enqueue(self, item):\n",
    "        self.items.insert(0,item)\n",
    "    def dequeue(self):\n",
    "        return self.items.pop()\n",
    "    def size(self):\n",
    "        return len(self.items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def josephRing(nameArr,n):\n",
    "    nameQueue = Queue()\n",
    "    for name in nameArr:\n",
    "        nameQueue.enqueue(name)\n",
    "    \n",
    "    while nameQueue.size()>1:\n",
    "        for i in range(n):\n",
    "            nameQueue.enqueue(nameQueue.dequeue())\n",
    "        nameQueue.dequeue()\n",
    "    \n",
    "    return nameQueue.dequeue()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c\n"
     ]
    }
   ],
   "source": [
    "print(josephRing(['a','b','c','d','e','f'],7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Deque(object):\n",
    "    def __init__(self):\n",
    "        self.items = []\n",
    "    def size(self):\n",
    "        return len(self.items)\n",
    "    def addHead(self, item):\n",
    "        self.items.insert(0, item)\n",
    "    def addTail(self, item):\n",
    "        self.items.append(item)\n",
    "    def removeHead(self):\n",
    "        return self.items.pop()\n",
    "    def removeTail(self):\n",
    "        return self.items.pop(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def checkHuiwen(srcStr):\n",
    "    deque = Deque()\n",
    "    for ch in srcStr:\n",
    "        deque.addHead(ch)\n",
    "    \n",
    "    while deque.size()>1:\n",
    "        if deque.removeHead()!=deque.removeTail():\n",
    "            return False\n",
    "    \n",
    "    return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "checkHuiwen('a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "1c836ea1838598e769062a9cf5fa60bfd2e0efc946eee0512711b4e28a236389"
  },
  "kernelspec": {
   "display_name": "Python 3.8.13 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
